<%--@elvariable id="myLoginId" type="java.lang.String"--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <%@include file="../../includes/common.jsp" %>
    <title>问题列表</title>
</head>
<body>
<div id="mainContent" class="easyui-panel" data-options="fit:true" style="padding: 10px">
    <form class="form-inline" action="">

        <label for="questionContent">问题内容</label>
        <input type="text" class="input-medium" name="questionContent" id="questionContent" value=""/>

        <a class="btn btn-primary" onclick="query()">查询</a>
        <a class="btn btn-primary" onclick="addQuestion()">新增</a>
        <a class="btn btn-primary" onclick="delQuestions()">删除</a>
    </form>
    <table id="dataTable"></table>
    <div id="dataEdit" closed="true" modal="true" style="overflow: hidden">
        <iframe style="width: 100%;height: 100%"
                id="contentFrame"
                name="contentFrame"
                marginheight="0"
                marginwidth="0"
                frameborder="0" src=""
                allowTransparency="true">
        </iframe>
    </div>
</div>
</body>
</html>

<script type="text/javascript">
    function query() {
        listOption.url = basePath + "question/manager/listAllQuestionDo";
        listOption.data = "questionContent="+$("#questionContent").val().trim();
        listOption.pageNumber = 1;
        loadData(listOption);
    }

    function addQuestion() {
        var contentFrame = $("#contentFrame");
        var dataEdit = $("#dataEdit");
        contentFrame[0].contentWindow.document.write("");
        contentFrame.attr("src", basePath + "question/manager/addAsk");
        dataEdit.dialog({
            title: '新增',
            height: 400,
            width: 600
        });
        dataEdit.dialog('open');
    }

    function selectAll() {
        if ($("#checkAll").attr("checked") == "checked") {
            $("[name='questionCheckBox']").attr("checked", true);
        } else {
            $("[name='questionCheckBox']").attr("checked", false);
        }
    }

    function initDataGrid() {
        $('#dataTable').datagrid({
            title: "问题列表",
            pagination: true,
            fitColumns: true,
            columns: [
                [
                    {field: 'id', title: "&nbsp<input id='checkAll' type='checkbox' onclick='selectAll()'/>", align: 'center', width: 200},
                    {field: 'userName', title: '提问者', sortable: true, align: 'center', width: 200},
                    {field: 'questionContent', title: '问题内容', align: 'center', width: 200},
                    {field: 'askDate', title: '提问时间', sortable: true, align: 'center', width: 200},
                    {field: 'operate', title: '操作', align: 'center', width: 200}
                ]
            ],
            sortName: "",
            sortOrder: "",
            onSortColumn: onSortColumn
        });
    }

    function onSortColumn(sortColumn, sortDirection) {
        switch (sortColumn) {
            case "askDate":
                sortColumn = "ask_date";
                break;
        }
        onSortColumnDefault(sortColumn, sortDirection);
    }

    function format(data) {
        data.resultList = queryFormat(data.resultList);
        return data;
    }

    function queryFormat(temp) {
        for (var i in temp) {
            var id = "&nbsp;<input type='checkbox' name='questionCheckBox' id='" + temp[i].id + "' value='" + temp[i].id + "'\">&nbsp;";
            var operate = "<a onclick='editQuestion(\"" + temp[i].id + "\")'>编辑</a>&nbsp;&nbsp;"
                    + "<a onclick='delQuestion(\"" + temp[i].id + "\")'>删除</a>&nbsp;&nbsp;"
                    + "<a onclick='findAnswer(\"" + temp[i].id + "\")'>查看</a>&nbsp;&nbsp;";
            temp[i].id = id;
            temp[i].operate = operate;
        }
        return temp;
    }

    function findAnswer(id) {
        var contentFrame = $("#contentFrame");
        var dataEdit = $("#dataEdit");
        contentFrame[0].contentWindow.document.write("");
        contentFrame.attr("src", basePath + "question/manager/listAllAnswer?id=" + id);
        dataEdit.dialog({
            title: '编辑',
            height: 400,
            width: 1000
        });
        dataEdit.dialog('open');
    }

    function editQuestion(id) {
        var contentFrame = $("#contentFrame");
        var dataEdit = $("#dataEdit");
        contentFrame[0].contentWindow.document.write("");
        contentFrame.attr("src", basePath + "question/manager/editAsk?id=" + id);
        dataEdit.dialog({
            title: '编辑',
            height: 400,
            width: 600
        });
        dataEdit.dialog('open');
    }


    function delQuestions() {
        var ids = getSelectId();
        if (ids == "") {
            $.messager.alert("提示", "请选择问题！");
            return;
        }
        delQuestion(ids);
    }

    function getSelectId() {
        var ids = "";
        var value;
        var allBox = $("[name='questionCheckBox']");
        for (var i = 0; i < allBox.length; i++) {
            value = allBox[i].getAttribute("value");
            if ($("#" + value).attr("checked") == "checked") {
                ids += value + ",";
            }
        }
        if ("" != ids) {
            ids = ids.substr(0, ids.length - 1);
        }
        return ids;
    }

    function delQuestion(id) {
        $.messager.confirm("提示", "确认删除？", function (r) {
            if (r) {
                $.ajax({
                    url: basePath + "question/ask/delAsk?ids=" + id,
                    dataType: "json",
                    type: "DELETE",
                    success: function () {
                        $.messager.alert("成功", "删除成功！", "", function () {
                            query();
                        })
                    },
                    error: doError
                })
            }
        })
    }

    $(function () {
        initDataGrid();
        query();
    });

</script>